package src.origintrail;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author csmythe
 */
public class EventRandom extends Event{
    private Random r;
    
    public static void main(String[] args){
        EventRandom e = new EventRandom();
        try{
            e.activate();
        }catch(Exception u){
            u.printStackTrace();
        }
    }

    public EventRandom() {
        r = new Random();
    }
	
	/*------
	 Random Events
	 ------
	 if you don't have >2 clothes/party member in summer, or >3 clothes/party member in winter, chance of catching the cold or flu
	 chance of catching dysentary
	 bear attacks camp, steals food
	 thieves steal from wagon
	 wagon breaks
	 oxen dies
	 find a dead wagon, +supplies
	 find a bounty of food
	 find a fellow wagon party, no chance of thieves for 50 miles CHRIS FIX THIS
*/	 

    @Override
    public void activate() throws Exception {
        int type = r.nextInt(1000000);
        String s = "";
        if(type < 20000){
            int a = -200-r.nextInt(100);
            s = "You were attacked by a bear. You lose " + Math.min(-a, OriginTrail.plr.bags.find(new Food()).getStack()) + " food.";
            EvtSpawner.instance.getHandler().add(new EventBuy(new Food().initialize(a)));
            EvtSpawner.instance.getHandler().pop().activate();
        }else if(type < 20300){
            int mem = r.nextInt(OriginTrail.plr.getParty().length);
            s = OriginTrail.plr.partyMemberDies(mem) + "got run over by the wagon and died.";
        }else if(type < 44000){
            int add = r.nextInt(400);
            s = "You found a wonderful fishing hole. You gain " + add + " food.";
            EvtSpawner.instance.getHandler().add(new EventBuy(new Food().initialize(add)));
            EvtSpawner.instance.getHandler().pop().activate();
        }else if(type < 50000){
            int add = r.nextInt(500);
            s = "You found a hidden box with some money inside. You gain " + add + " money.";
            EvtSpawner.instance.getHandler().add(new EventBuy(new Money().initialize(add)));
            EvtSpawner.instance.getHandler().pop().activate();
        }else if(type < 80000){
            int add = -100-r.nextInt(100);
            s = "A thief stole some of your money. You lost " + Math.min(OriginTrail.plr.bags.find(new Money()).getStack(), -add)+ " money";
            EvtSpawner.instance.getHandler().add(new EventBuy(new Money().initialize(add)));
            EvtSpawner.instance.getHandler().pop().activate();
        }else if(type < 80001){
            s = "You have died!";
            EvtSpawner.instance.spawnDieEvent();
            EvtSpawner.instance.getHandler().pop().activate();
        }else if(type < 90000){
            OriginTrail.plr.setHealth(OriginTrail.plr.getHealth() - 10 - r.nextInt(10));
            if(OriginTrail.plr.getHealth() <= 0){
                EvtSpawner.instance.spawnDieEvent();
                EvtSpawner.instance.getHandler().pop().activate();
            }
            s = "Your wagon was damaged! It has " + OriginTrail.plr.getHealth() + " hp left.";
        }else if(type < 100000){
            if(OriginTrail.plr.bags.find(new Oxen()).getStack() > 0){
                OriginTrail.plr.bags.find(new Oxen()).add(-1);
                s = "An ox has died!";
            }
            
            
        }
        if(!s.equals("")){
            Map.stop = true;
            ModuleMove.redoLabels();
        
            JFrame j = new JFrame();
            j.getContentPane().setLayout(new BoxLayout(j.getContentPane(), BoxLayout.Y_AXIS));
            JButton b = new JButton("ok");
            b.addActionListener(new OKListener(j));
            j.add(new JLabel(s));
            j.add(b);
            j.pack();
            j.setVisible(true);
        }
            
    }

    private static class OKListener implements ActionListener {
        JFrame parent;

        public OKListener(JFrame j) {
            parent = j;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            parent.dispose();
        }
    }
}
